package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/28 19:19
 */
public class GroupNoticeSetView extends IView {

    private GroupNoticeSetCallback mCallback;

    public GroupNoticeSetView(GroupNoticeSetCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doSetGroupNotice(String groupId, String newGroupNotice) {
        GroupDto groupDto = new GroupDto(UserPreferences.getUserType());
        groupDto.setGroupId(groupId);
        groupDto.setAnnouncementContent(newGroupNotice);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_ANNOUNCEMENT_UPDATE_REGISTERED : EventConstants.Action.GROUP_ANNOUNCEMENT_UPDATE_TOURISTS;
        doSendRequest(action, groupDto.toJson());
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print("GroupNoticeSetView", event.toString());
        if (event.action() == EventConstants.Action.GROUP_ANNOUNCEMENT_UPDATE_TOURISTS ||
                event.action() == EventConstants.Action.GROUP_ANNOUNCEMENT_UPDATE_REGISTERED) {
            if (mCallback != null)
                mCallback.onSetGroupNotice(event.code(), event.msg());
        }
    }

    public interface GroupNoticeSetCallback {

        void onSetGroupNotice(int code, String msg);
    }
}
